<?php
/**
 * Created by PhpStorm.
 * User: suruixiang
 * Date: 2017/10/12
 * Time: 上午11:07
 */
namespace app\admin\model;

use think\Model;

class CoursesCategoryRelation extends Model
{
    //连接趁早数据库
    protected $connection = 'cz_db_config';

    /**
     * 根据单字段获取信息
     * @param $user_name
     * @param string $fields
     * @return object
     */
    public function getInfoByField($field_name, $field_value, $fields = '*')
    {
        return $this->where($field_name, $field_value)->field($fields)->find();
    }

    /**
     * 获取单字段内容
     * @param $field_name
     * @param $field_value
     * @param $field
     * @return
     */
    public function getFileByField($field_name, $field_value, $field)
    {
        return $this->where($field_name, $field_value)->value($field);
    }

    /**
     * 获取课程所属分类名
     * @param $course_id
     * @return string
     */
    public function getCategoryByCourseId($course_id)
    {
        $category_names = '';
        $category_ids = $this->where([
            'courseId' => $course_id,
            'type'     => 1,
            'disabled' => 0
        ])->column('categoryId');
        if ($category_ids) {
            $categoryModel = new Category();
            $category_names = $categoryModel->getNameById($category_ids);
        }
        return $category_names;
    }

    /**
     * 根据分类ID获取课程ID
     * @param $category_id
     * @return string
     */
    public function getCourseIdByCategoryId($category_id)
    {
        $ids = $this->where([
            'categoryId' => $category_id,
            'type'     => 1,
            'disabled' => 0
        ])->column('courseId');
        return implode(',', $ids);
    }
}